package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author whx
 * @since 2024-12-02
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {

    @Override
    public void updateOrInsert(BankAccountUpsertReqDTO dto) {
        BankAccount bankAccount = BeanUtils.toBean(dto, BankAccount.class);
        if(dto.getId()==null){
            bankAccount.setId(UserContext.currentUserId());
        }

        Long userId = UserContext.currentUserId();
        BankAccount bankAccount1 = baseMapper.selectById(userId);
        if(ObjectUtil.isNotNull(bankAccount1)){
            baseMapper.deleteById(userId);
        }

        baseMapper.insert(bankAccount);
    }

    @Override
    public BankAccountResDTO currentUserBankAccount() {

        BankAccount account = this.lambdaQuery()
                .eq(BankAccount::getId, UserContext.currentUser().getId())
                .one();

        return BeanUtils.toBean(account, BankAccountResDTO.class);
    }
}
